<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

/**
 *
 *
 *
 * @package     ceo
 * @subpackage  models
 * @category    models
 * @author      ydd team, wu
 * @copyright   copyright (c) 2012, yaoduoduo, Inc.
 * @filesource
 */
class Users_model extends Base_model {

	protected $_table_name = 'users';
	protected $_key_name = 'users_id';

	function get_user($limit = "") {
		if ($limit != "")
			$this -> set_limit($limit);
		$this -> db -> join('role', 'users.role_id = role.role_id');
		$where = array('users_dtime' => 0);
		return $this -> fetch(NULL, $where);
	}

	function get_user_by_user($name) {

		$this -> db -> like('name', $name);
		$query = $this -> db -> get($this -> _table_name);

		return $query;

	}

	function get_user_by_broker($users_id) {

		$sql = " SELECT * FROM (`h_users`) WHERE  `users_id` = '" . $users_id . "'";
		$query = $this -> db -> query($sql);

		return $query;

	}

	function get_user_s_name($name) {

		$sql = "SELECT * FROM  `h_users`  WHERE `name` like '%" . $name . "%'";
		$query = $this -> db -> query($sql);
		return $query;

	}

	function get_user_s_user($user) {

		$this -> db -> where('user', $user);
		$query = $this -> db -> get($this -> _table_name);
		return $query;

	}

	function get_user_count() {
		$con["where"][] = array('users_dtime' => 0);
		$this -> set_args_array($con);
		return $this -> fetch_count();
	}

	function get_users($keyword) {

		if ($keyword != "")
			$this -> set_or_like('name', $keyword);
		$where = array('users_dtime' => 0);
		$result = $this -> fetch(NULL, $where);
		$p = array();
		foreach ($result as $key => $val) {
			$p[$val -> users_id]["name"] = urlencode($val -> name);
		}
		return $p;
	}

	function get_all_users() {

		$this -> db -> where('users_dtime', 0);
		$p = $this -> db -> get($this -> _table_name);
		return $p -> result();
	}

	function get_all() {

		//$this -> db -> where('users_dtime', 0);
		$sql="SELECT * FROM `h_users` where `users_dtime` ='0' ";
		$p = $this -> db -> query($sql);
		return $p -> result();
	}

	function query_user($where, $keyword = null, $limit) {
		$this -> set_or_like('user,name,mobile,email', $keyword);
		$this -> set_limit($limit);
		$where['users_dtime'] = 0;
		return $this -> fetch(NULL, $where);
	}

	function query_user_count($where, $keyword = null) {
		$this -> set_or_like('user,name,mobile,email', $keyword);
		$where['users_dtime'] = 0;
		$this -> set_args_array($where);
		return $this -> fetch_count();
	}

	function check_login_user($data) {
		
		
		if (!empty($data -> user))
			$where['user'] = $data -> user;
		if (!empty($data -> password))
			$where['password'] = MD5($data -> password);
		$where['users_dtime'] = 0;
		
		return $this -> fetch(NULL, $where, NULL, 'row');
		
	}

	function get_user_info($id) {
		if (!empty($id))
			$where[$this -> _key_name] = $id;
		$where['users_dtime'] = 0;
		return $this -> fetch(NULL, $where, NULL, 'row');
	}

	function update_user($data) {

		$value = array('department_id' => $data['department_id'], 'role_id' => $data['role_id'], 'email' => $data['email'], 'phone' => $data['phone'], 'name' => $data['name'], 'users_mtime' => time());
		$where[] = array($this -> _key_name => $data['id']);
		return $this -> update($value, $where);
	}

	function update_user_password($data) {
		$value = array('password' => md5($data['password']));
		$where[] = array($this -> _key_name => $data['id']);
		return $this -> update($value, $where);
	}

	function add_user($data) {
		$data['users_ctime'] = time();
		return $this -> insert($data);
	}

	function del_user($data) {
		$where[] = array($this -> _key_name => $data['id']);
		$value = array('users_dtime' => time());
		return $this -> update($value, $where);
	}

	public function check_add_user($user_name = null) {
		if ($user_name == null) {
			return false;
		}
		$this -> db -> where('user', $user_name);
		if ($this -> fetch_row() == null) {
			return true;
		} else {
			return false;
		}
	}

	public function acl_login_handle() {
		$data = array();
		$data['login_ip'] = $this -> input -> ip_long();
		$data['ltime'] = time();
		$this -> update($data);
	}

}
